/*
 * @(#)MaintDmsPermissionAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsValidation;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.DocumentValidateManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;

/**
  MaintDmsPermissionAction.java

  This class is for maint dms root.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   13/08/2003
    @version         $Revision: 1.13 $
*/

public class MaintDmsPermissionAction extends com.dcivision.setup.web.AbstractMaintPermissionAction {

  public static final String REVISION = "$Revision: 1.13 $";

  public MaintDmsPermissionAction() {
    super();
    this.objectIDProperty = "objectID";
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsDocumentDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_ADMIN);
  }

  /**
   * clearUniqueFields
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void clearUniqueFields(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {

  }


  /**
   * clearUniqueFields
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void init(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.init(mapping, form, request, response);

    MaintDmsPermissionForm actionForm = (MaintDmsPermissionForm)form;
    com.dcivision.user.dao.UserPermissionInheritDAObject userPermInheritDAO = new com.dcivision.user.dao.UserPermissionInheritDAObject(this.getSessionContainer(request), this.getConnection(request));
    com.dcivision.user.bean.UserPermissionInherit tmpUserPermissionInherit = (com.dcivision.user.bean.UserPermissionInherit)userPermInheritDAO.getObjectByObjectTypeObjectID(actionForm.getObjectType(), new Integer(actionForm.getObjectID()));
    if (tmpUserPermissionInherit == null) {
      actionForm.setInheritanceFlag(GlobalConstant.TRUE);
    } else {
      actionForm.setInheritanceFlag(null);
    }
  }

  /**
   * insertRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void insertRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    //update all sessionctn permission string in cache
    MaintDmsPermissionForm actionForm = (MaintDmsPermissionForm)form;
    PermissionManager.firePermissionChanged(GlobalConstant.OBJECT_TYPE_DOCUMENT,new Integer(actionForm.getObjectID()));
    
    this.updateInheritanceFlag(mapping, form, request, response);
    this.updatePermissionObjects(mapping, form, request, response);
  }

  /**
   * updateRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    //update all sessionctn permission string in cache
    MaintDmsPermissionForm actionForm = (MaintDmsPermissionForm)form;
    PermissionManager.firePermissionChanged(GlobalConstant.OBJECT_TYPE_DOCUMENT,new Integer(actionForm.getObjectID()));
    this.updateInheritanceFlag(mapping, form, request, response);
    this.updatePermissionObjects(mapping, form, request, response);
  }

  /**
   * updateInheritanceFlag
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void updateInheritanceFlag(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintDmsPermissionForm actionForm = (MaintDmsPermissionForm)form;
    boolean inheritFlag = GlobalConstant.TRUE.equals(actionForm.getInheritanceFlag());

    com.dcivision.user.dao.UserPermissionInheritDAObject userPermInheritDAO = new com.dcivision.user.dao.UserPermissionInheritDAObject(this.getSessionContainer(request), this.getConnection(request));

    if (inheritFlag) {
      userPermInheritDAO.deleteObjectByObjectTypeObjectID(actionForm.getObjectType(), new Integer(actionForm.getObjectID()));
    } else {
      com.dcivision.user.bean.UserPermissionInherit tmpUserPermissionInherit = new com.dcivision.user.bean.UserPermissionInherit();
      tmpUserPermissionInherit.setObjectType(actionForm.getObjectType());
      tmpUserPermissionInherit.setObjectID(new Integer(actionForm.getObjectID()));
      tmpUserPermissionInherit.setInheritanceFlag(GlobalConstant.FALSE);
      tmpUserPermissionInherit.setRecordStatus(GlobalConstant.RECORD_STATUS_ACTIVE);
      userPermInheritDAO.updateObject(tmpUserPermissionInherit);
    }
  }

  /**
   * copyRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void copyRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
  }

  /**
   * deleteRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void deleteRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintDmsPermissionForm actionForm = (MaintDmsPermissionForm)form;
    com.dcivision.user.dao.UserPermissionInheritDAObject userPermInheritDAO = new com.dcivision.user.dao.UserPermissionInheritDAObject(this.getSessionContainer(request), this.getConnection(request));
    userPermInheritDAO.deleteObjectByObjectTypeObjectID(actionForm.getObjectType(), new Integer(actionForm.getObjectID()));
    this.updatePermissionObjects(mapping, form, request, response);
  }

  /**
   * getObjectType
   *
   * @return  The corresponding object type of permission setup action.
   */
  public String getObjectType() {
    return(GlobalConstant.OBJECT_TYPE_DOCUMENT);
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    MaintDmsPermissionForm actionForm = (MaintDmsPermissionForm)form;

    if (!Utility.isEmpty(actionForm.getCurFunctionCode())) {
      this.setFunctionCode(actionForm.getCurFunctionCode());
    }

    //Add By Jim Zhou       2007/01/10       EIP-1292
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    Integer documentID = TextUtility.parseIntegerObj(actionForm.getObjectID());
    DmsDocument document = docRetrievalManager.getDocument(documentID);
    DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);
    StringBuffer message = new StringBuffer();

    DmsValidation validation = docValidateManager.validateOperateAction(document, false, "B");
    List lstIdMisRight = validation.getLstIdReject();
    List lstIdBeLocked = validation.getLstIdlocked();
    List lstIdBeDeleted = validation.getLstIdDeleted();
    List lstIdBeArchived = validation.getLstIdArchived();
    List lstIdRelationship = validation.getLstIdHaveRelationship();
    if (!lstIdBeArchived.isEmpty()){
      for (int i=0; i<lstIdMisRight.size(); i++) {
        this.addError(request,"errors.dms.no_permission",docRetrievalManager.getLocationPath((Integer)lstIdMisRight.get(i)));
      }
      for (int i=0; i<lstIdBeLocked.size(); i++) {
        this.addError(request,"errors.dms.been_checkout",docRetrievalManager.getLocationPath((Integer)lstIdBeLocked.get(i)));
      }
      for (int i=0; i<lstIdBeDeleted.size(); i++) {
        this.addError(request,"errors.dms.been_deleted",docRetrievalManager.getLocationPath((Integer)lstIdBeDeleted.get(i)));
      }
      for (int i=0; i<lstIdBeArchived.size(); i++) {
        this.addError(request,"errors.dms.been_archived",docRetrievalManager.getLocationPath((Integer)lstIdBeArchived.get(i)));
      }
      if (Utility.isEmpty(actionForm.getReturnTo())){
        return mapping.findForward("Home");
      } else {
        ActionForward actionForward = new ActionForward();
        actionForward.setPath("/dms/" + actionForm.getReturnTo());
        return actionForward;
      }
    }
    //Add ENd

    return super.execute(mapping, form, request, response);
  }
}
